<?php 

namespace app\index\controller;


use think\Controller;
use think\Request;
use think\Session;
use think\Db;
use think\Validate;
class Article extends Controller
{
    public function index()
    {  
        // $total=db('article')->count();
        //   $data=db('article')
        //      ->limit(3)
        //      ->order("ID DESC")
        //      ->select();
       $data = Db::name('article')
                ->group('sid')
                ->select();
        $a = [];           
    foreach ($data as $v) {
            $b=date('Y-m-d',$v['time']);
            $c = array_push($v,$b);
            $d = Db::name('sort')
                   ->field('sort')
                   ->where('id',$v['sid'])
                   ->find();
            $e = array_push($v,$d['sort']);
             $a[] = $v;
        }
    // var_dump($a);die;
        return view('',[
             'data'=>$a,
            ]);
        
    }
   
    //进入分类页面
    public function sort($id)
    {
     //根据文章的ID查到该文章的分类,然后根据分类查到该分类下所有的文章
      $a = Db::name('article')
                 ->field('sid')
                 ->where('id',$id)
                 ->find();
      $b = Db::name('sort')
             ->field('sort')
             ->where('id',$a['sid'])
             ->find();
      $c=$b['sort'];
     $data = Db::name('article')
              ->where('sid',$a['sid'])
              ->select();
    
    // dump($data);die;
      return view('',[
             'data'=>$data,
             'c'=>$c
            ]);



    }

   //详情页面
    public function details($id)
    {  
        // var_dump('123');die;
        $data = Db::name('article')
                  ->where('id ='.$id)
                  ->find();
        $data['time']=date('Y-m-d',$data['time']);
        // var_dump($data);die;

       //查出所有评论表里面该文章的数据
        $res = Db::name('comment')
                 ->where('artid',$id)
                 ->select();
        // var_dump($res);die;
         $a=[];
        foreach ($res as $v) {
            //给数组放时间格式的时间
            if($v['status']===1){
           $b=date('Y-m-d',$v['time']);
           $c = array_push($v,$b);
           //给数组放用户名字
           $d=Db::name('vip')
                 ->field('username')
                 ->where('uid',$v['uid'])
                 ->find();
          $e = array_push($v,$d['username']);
          //给数组放文章名字
           $f=Db::name('article')
                 ->field('title')
                 ->where('id',$v['artid'])
                 ->find();
          $g = array_push($v,$f['title']);
            $a[] = $v;};
        };
     // var_dump($a);die;

      //判断登陆的人是否是评论者
      //如果是评论者显示删除按钮   不是的话显示空.
         $m= Session::get('user_info');
         $n = $m['uid'];
        // var_dump($n);die;
        return View('',[
            'result'=>$a,
            'data'=>$data
             ]);
 }





    //评论专区
    public function comment(Request $request)
    { 
      
      //接收数据
       $a=Session::get('user_info');
       
       if(empty($a))
        {return $this->error('您没有登录不能评论',url('index/Login/login'));}else{
      $data['uid']=$a['uid'];
      $data['time']=time();
      $p = $request->post();
       //验证
       $rule = [
          'comment' => 'require|max:250|chsDash',
      ];

       $msg = [
      'comment.require' => '评论内容不能为空',
      'comment.max' => '文章名称过长',
      'comment.chsDash'=>'文章题目只能是汉字、字母、数字和下划线_及破折号-',
      ];
      $data['comment']= trim($p['comment']);
      $data['artid']=$p['id'];
      

       $validate = new Validate($rule,$msg);
      if(!$validate->check($data)){
      $this->error($validate->getError());
      }
      
      $res = Db::name('comment')
               ->data($data)
               ->insert();
      if($res > 0){
                    return $this ->success('评论成功',url('index/article/index'));
                }else{

                    return $this->error('评论失败,请重试');
                }
     }

    }
    
    //评论区的删除问题
    public  function delete()
    {
      $res =Db::name('comment')->where(['id'=> input()['id']])->where(['uid'=>input()['uid']])->delete();
      if($res)
      {
        $arr['status'] =1;
        $arr['msg'] = '删除成功';
        return $arr;
      }else{
        $arr['status'] = 2;
        $arr['msg'] = '删除失败';
        return $arr;
      }      


    }

    //点赞
    public function zan()
    {
       $a=Session::get('user_info');
       
       

       if(empty($a))
        {
          $arr['status'] =1;
          $arr['msg'] = '请登录!';
          return $arr;
        }else{
          //判断 登录的这个人 有没有 点赞 这个 评论
          $res = Db::name('userzan')->where(['loginid'=> $a['uid']])->where(['cid'=>input()['id']])->select();

          if($res){
              // -1
               $e = Db::name('userzan')->where(['loginid'=> $a['uid']])->where(['cid'=>input()['id']])->delete();
              $a = Db::name('comment')->where(['id'=>input()['id']])->find();
              $num = $a['zan'];
              $num = $num -1;
              if($num<0){
                $num = 0;
              }
              $b = Db::name('comment')->where(['id'=>input()['id']])->update(['zan'=>$num]);
              $c = Db::name('comment')->where(['id'=>input()['id']])->find();
              $d = $c['zan'];

            $arr['status'] =2;
            $arr['msg'] = $d;
            return $arr;
          }else{
               $a = Db::name('comment')->where(['id'=>input()['id']])->find();
              $num = $a['zan'];
              $num = $num +1;
            
              $b = Db::name('comment')->where(['id'=>input()['id']])->update(['zan'=>$num]);
              $brr = [];
              $brr['loginid'] = Session::get('user_info')['uid'];
              $brr['uid'] = input()['uid'];
              $brr['cid'] = input()['id'];
              $e = Db::name('userzan')->insert($brr); 
              $c = Db::name('comment')->where(['id'=>input()['id']])->find();
              $d = $c['zan'];

            $arr['status'] =3;
            $arr['msg'] = $d;
            return $arr;
          }
          
          // $res = Db::name('userzan')->where(['loginid'=>$a])->where(['cid'=>input()['id']])->select();

          
        }


    }





}
 ?>